<?php

namespace app\storeapi\model\store\shop;

use app\common\model\store\shop\Clerk as ClerkModel;
use utils\Random;
/**
 * 商家门店店员模型
 * Class Clerk
 * @package app\store\model\store\shop
 */
class Clerk extends ClerkModel
{
    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';

    /**
     * 获取列表数据
     * @param int $param 状态
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        $page = isset($param['page']) && is_numeric($param['page']) ? intval($param['page']) : 1;
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $param['shop_id'] > 0 && $this->where('shop_id', '=', (int)$param['shop_id']);
        // 查询列表数据
        return $this->with(['user'])
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->field('clerk_id,shop_id,user_id,real_name,account,mobile,status')
            ->limit($offset, $limit)
            ->select();

    }

    /**
     * 代言人状态
     * @param $value
     * @return mixed
     */
    public function getStatusAttr($value)
    {
        $status = [0 => '禁用', 1 => '启用'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_ADD)) {
            return false;
        }
        $data['salt'] =Random::alnum();
        $data['password'] =md5(md5($data['password']. $data['salt'].config('salt')));

        //$data['password'] = yoshop_hash($data['password']);
        $data['openid'] = db('user')->where(['user_id'=>$data['user_id']])->value('open_id');
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|false|int
     * @throws \think\exception\DbException
     */
    public function edit($data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_EDIT)) {
            return false;
        }
        if(!trim($data['password'])){
            unset($data['password']);
        }else{
           // $data['password'] = yoshop_hash($data['password']);
             $data['salt'] =Random::alnum();
             $data['password'] =md5(md5($data['password']. $data['salt'].config('salt')));
             
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     * @throws \think\exception\DbException
     */
    private function validateForm($data, $scene = self::FORM_SCENE_ADD)
    {
        if ($scene === self::FORM_SCENE_ADD) {
            if (!isset($data['user_id']) || empty($data['user_id'])) {
                $this->error = '请选择用户';
                return false;
            }

            if (db('store_shop_clerk')->where(['account' => $data['account'], 'is_delete' => 0])->find()) {
                $this->error = '该账号已存在，无需重复添加';
                return false;
            }
            if (self::detail(['user_id' => $data['user_id'], 'is_delete' => 0])) {
                $this->error = '该用户已经是店员，无需重复添加';
                return false;
            }
        }elseif($scene === self::FORM_SCENE_EDIT){
            if (!isset($data['user_id']) || empty($data['user_id'])) {
                $this->error = '请选择用户';
                return false;
            }
            $flag = db('store_shop_clerk')
                ->where(['account' => $data['account'], 'is_delete' => 0,'clerk_id'=>['neq',$data['clerk_id']]])
                ->find();
            if ($flag) {
                $this->error = '该账号已存在';
                return false;
            }
        }
        return true;
    }

}